(function($){
	display("conf[PUBLIC_STATUS]"); // 需要翻译的类型
    $(function(){
        $('#confEditForm').validator({
            theme:'yellow_right_effect',
            stopOnError: false,
            timely: 1,
            ignore: ':hidden',
            fields: {
                'cfgId': 'ID:required;',
                'cfgKey' : 'KEY:required;',
                'cfgVal': 'VAL:required;'
            },
            valid: function(){
                $('#confEditForm')._ajaxForm(function(r){
                    if(r.r){$("#searchButton").click();}else{return false;}
                    asyncbox.tips(r.m, r.r ? 'success' : 'error');
                });
            }
        });

        var oTable = $('#datatable').dataTable({
            bProcessing:true,
            "sAjaxSource" : window.basePath + "/conf/list.html",
            "fnServerParams": function (aoData) {
                var search = $('#searchForm')._formToJson();
                for(var i in search) {
                    aoData.push({"name" : i, "value" : search[i]});
                }
            },
            "aoColumnDefs": [
              {"aTargets" : [ 0 ],"mData" : "cfgId"},
              {"aTargets" : [ 1 ],"mData" : "cfgKey"},
              {"aTargets" : [ 2 ],"mData" : "cfgVal"},
              {"aTargets" : [ 3 ],"mData" : "remark"},
              {"aTargets" : [ 4 ],"mData" : "parentCfg", mRender : function(data, type, full){
                  return data ? data : '--';
              }},
              {"aTargets" : [ 5 ],"mData" : "parentKey", mRender : function(data, type, full){
                  return data ? data : '--';
              }},
              {"aTargets" : [ 6 ],"mData" : "sort"},
              {"aTargets" : [ 7 ],"mData" : "status", mRender : function(data, type, full){
                  return $.fn.display.conf.PUBLIC_STATUS[data];
              }},
              {"aTargets" : [ 8 ],"mData" : null, sClass:'text-center', mRender : function(data, type, full) {
                  return '<a class="btn btn-default btn-xs edit" href="javascript:void(0);"><i class="icon-edit"></i> 修改</a>&nbsp;'
                        +'<a class="btn btn-danger btn-xs delete" href="javascript:void(0);" cfgId="' + full.cfgId + '" cfgKey="' + full.cfgKey + '"><i class="icon-trash"></i> 删除</a>';
              }}
            ]
        });
        $("#searchButton").click(function(){
            $('#datatable').dataTable()._fnAjaxReload();
        });
        $('#newConfig').click(function(){
            $('#cfgKey,#cfgId').removeAttr('readonly');
            $('#confEditForm').attr('action',window.basePath + '/conf/add.html').resetForm();
            $.modalDialog({
                id:'confEditDialog',
                title:'新建配置',
                ok:function(){
                    $('#confEditForm').submit();
                }
            });
        });
        /* 修改 */
        $('#datatable').on('click','a.edit', function(e) {
            $('#cfgKey,#cfgId').attr('readonly', 'readonly');
            var tr = $(this).parents('tr:first');
            var aData = oTable.fnGetData(tr[0]);
            $('#confEditForm').attr('action',window.basePath + '/conf/upd.html').resetForm()._jsonToForm(aData);
            $.modalDialog({
                id:'confEditDialog',
                title:'修改配置',
                ok:function(){
                    $('#confEditForm').submit();
                }
            });
        });
        /* 删除 */
        $('#datatable').on('click','a.delete', function(e) {
            var cfgId = $(this).attr("cfgId");
            var cfgKey = $(this).attr("cfgKey");
            $.modalConfirm({
                id:'modal-confirm',
                content:'确定要删除该配置吗？',
                ok: function(){
                    $._ajaxPost(window.basePath + '/conf/del.html',{cfgId:cfgId,cfgKey:cfgKey},function(r){
                        if(r.r){$("#searchButton").click();}
                        asyncbox.tips(r.m,r.r ? 'success' : 'error');
                    });
                }
            });
        });
    });
})(jQuery);